package com.webbuilder.task;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.jiusit.golf.api.HtmlParser;
import com.jiusit.golf.api.LotteryUtil;
import com.webbuilder.common.Var;
import com.webbuilder.utils.DateUtil;
import com.webbuilder.utils.DbUtil;
import com.webbuilder.utils.StringUtil;
import com.webbuilder.utils.SysUtil;

public class GetLottery implements Job {
	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		System.out.println("开始获取");
		Connection conn = null;
		PreparedStatement st = null;
		PreparedStatement sel_st = null;
		PreparedStatement count_st = null;
		PreparedStatement update_st = null;
		PreparedStatement updateNum_st = null;
		PreparedStatement check_st = null;
		ResultSet sel_rs = null;
		ResultSet count_rs = null;
		ResultSet check_rs = null;
		boolean is_run = DateUtil.isTimeRang("02:00", "09:00");
		boolean is_get = true;
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if (!is_run) {
			int issue = 1;
			String insertSql = "INSERT INTO purchase_lottery (id, issue, lottery_numbers, create_date, periods, winning_number,count_num)";
			insertSql += " VALUES (?, ?, ?, now(), ?, ?,1)";
			String updateSql = "update purchase_lottery set lottery_numbers=?,winning_number=?,count_num=count_num+1 where id = ?";
			String updateNumSql = "update purchase_lottery set count_num=count_num+1 where id = ?";

			try {
				conn = DbUtil.getConnection();
				String date = DateUtil.formatDate(new Date(), "yyyyMMdd");
					date = DateUtil.formatDate(
							DateUtil.dateAdd(3, -1, new Date()), "yyyyMMdd");
					String countSql = "select count(*) as 'countNum', (select concat(lottery_numbers,',',count_num)  from purchase_lottery where periods='"+date+"' order by create_date desc limit 0,1) as 'lottery_count' from purchase_lottery where periods='"
							+ date + "'";
					count_st = conn.prepareStatement(countSql);
					count_rs = count_st.executeQuery();
					if (count_rs.next()) {
						String lottery_count=count_rs.getString("lottery_count");
						String[] data=lottery_count.split(",");
						String lottery_numbers=data[0];
						String count_num=data[1];
						if (count_rs.getInt("countNum") == 120) {
							if(StringUtil.isEmpty(lottery_numbers) && StringUtil.isEqual(count_num, "3")){
								date = DateUtil.formatDate(new Date(), "yyyyMMdd");
							}else{
								if(!StringUtil.isEmpty(lottery_numbers)){
									date = DateUtil.formatDate(new Date(), "yyyyMMdd");
								}
							}
						}
					}
				System.out.println("期数:" + date);
				String periodStr = date.substring(2, date.length());
				String selSql = "select * from purchase_lottery where periods='"
						+ date + "' order by create_date desc LIMIT 0,1 ";
				st = conn.prepareStatement(insertSql);
				update_st = conn.prepareStatement(updateSql);
				updateNum_st = conn.prepareStatement(updateNumSql);
				sel_st = conn.prepareStatement(selSql);
				sel_rs = sel_st.executeQuery();
				if (sel_rs.next()) {
					int i = sel_rs.getInt("issue");
					if(!StringUtil.isEmpty(sel_rs.getString("lottery_numbers")) && sel_rs.getInt("count_num") <= 3){
					issue = i + 1;
					
					}else if(StringUtil.isEmpty(sel_rs.getString("lottery_numbers")) && sel_rs.getInt("count_num") == 3){
						issue = i + 1;
						
					}else {
						issue = i ;
					}
					periodStr = periodStr + String.format("%03d", issue);
				} else {
					periodStr = periodStr + "001";

				}
				System.out.println("日期期号:" + periodStr);
				if (is_get) {
					String lottery_numbers = HtmlParser.getHtmlContent(date,
							"utf-8", periodStr);
					int winning_number = 0;
					if (StringUtil.isEmpty(lottery_numbers)) {
						lottery_numbers = "";
					} else {
						winning_number = Integer.parseInt(lottery_numbers);
					}

					String checkSql = "select * from purchase_lottery where periods='"
							+ date
							+ "' and issue='"
							+ issue
							+ "' and count_num < 3";
					check_st = conn.prepareStatement(checkSql);
					check_rs = check_st.executeQuery();
					if (check_rs.next()) {
						if (StringUtil.isEmpty(check_rs
								.getString("lottery_numbers"))) {
							System.out.println("中奖号:" + lottery_numbers);
							update_st.setString(1, lottery_numbers);
							update_st.setLong(2, winning_number);
							update_st.setString(3, check_rs.getString("id"));
							update_st.executeUpdate();
						}else{
							updateNum_st.setString(1, check_rs.getString("id"));
							updateNum_st.executeUpdate();
						}

					} else {
						System.out.println("中奖号:" + lottery_numbers);
						st.setString(1, SysUtil.getUUID());
						st.setLong(2, issue);
						st.setString(3, lottery_numbers);
						st.setString(4, date);
						st.setLong(5, winning_number);
						st.executeUpdate();
					}
				}
			} catch (Throwable e) {
				DbUtil.closeConnection(conn, true);
			} finally {
				DbUtil.closeStatement(st);
				DbUtil.closeStatement(count_st);
				DbUtil.closeStatement(update_st);
				DbUtil.closeStatement(updateNum_st);
				DbUtil.closeStatement(sel_st);
				DbUtil.closeResultSet(sel_rs);
				DbUtil.closeResultSet(count_rs);
				DbUtil.closeResultSet(check_rs);
				DbUtil.closeConnection(conn, false);
			}
		}
	}
}